package cn.edu.neu.softlab633.influencemaximization.sy.datapreprocessing;

import cn.edu.neu.softlab633.influencemaximization.sy.Index.NetworkIndex;
import cn.edu.neu.softlab633.influencemaximization.sy.Index.TopicIndex;
import cn.edu.neu.softlab633.influencemaximization.sy.datapreprocessing.InfluenceCal.InfluenceCal;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Map;

/**
 * Created by Jason on 2017/5/6.
 */
public class TwitterData implements Preprocessing {
    @Override
    public void topicPreprocessing() throws Exception {
        BufferedReader br = new BufferedReader(new FileReader("data/FT/twitter_username.csv"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("data/FT/twitter_topic_1.csv"));
        String line = br.readLine();
        while (line != null) {
            String id = line.split(",")[0];
            StringBuilder topic = new StringBuilder();
            for (int i = 0; i < ConstantTopic.topicNum; i++) {
                topic.append(Math.random() + " ");
            }
            bw.write(id + "," + topic);
            bw.newLine();
            line = br.readLine();
        }
        br.close();
        bw.close();
    }

    @Override
    public void relationPreprocessing() throws Exception {
        BufferedReader network_br = new BufferedReader(new FileReader("data/FT/twitter_network.csv"));
        BufferedWriter network_bw = new BufferedWriter(new FileWriter("data/FT/twitter_network_topic_1.csv"));
        // read twitter users' topic distribution
        long start = System.currentTimeMillis();
        Map<Integer, Double[]> userTopic = TopicIndex.getTopicIndex("data/FT/twitter_topic_1.csv");
        long end = System.currentTimeMillis();
        System.out.println("建立Twitter topic 索引耗时：" + (end - start) + "毫秒");
        // 构建入度的索引
        start = System.currentTimeMillis();
        Map<Integer, ArrayList<Integer>> idf = NetworkIndex.getReverseIndex("data/FT/twitter_network.csv");
        end = System.currentTimeMillis();
        System.out.println("建立Twitter network索引耗时：" + (end - start) + "毫秒");
        // read twitter network
        String line = network_br.readLine();
        line = network_br.readLine();
        while (line != null) {
            int id1 = Integer.valueOf(line.split(",")[0]);
            int id2 = Integer.valueOf(line.split(",")[1]);
            // calculate edge's influence
//            String influence = InfluenceCal.calEdgeWeight(id1, id2, userTopic, idf);
//            network_bw.write(id1 + "," + id2 + "," + influence);
            network_bw.newLine();
            line = network_br.readLine();
        }
        network_br.close();
        network_bw.close();
    }

//    private String calculateInfluence(int id1, int id2, Map<Integer, Double[]> userTopic, Map<Integer, ArrayList<Integer>> idf) {
//        StringBuilder influence = new StringBuilder();
//        Double[] topic1 = userTopic.get(id1);
//        ArrayList<Integer> in = idf.get(id2);
//        int size = in.size();
//        for (int i = 0; i < topic1.length; i++) {
//            double insum = 0;
//            for (int j = 0; j < size; j++) {
//                insum += userTopic.get(in.get(j))[i];
//            }
//            double tmp = 1.0 / 20 * (topic1[i] / insum);
//            influence.append(tmp + " ");
//        }
//        return influence.toString();
//    }
}
